
import { _decorator, Color, Component, Game, Label, Node, tween, Vec2, Vec3, } from 'cc';
import { GameSticker, GameStickerType } from '../../Model/GameStickerMgr';
import { UnitPlanetBase } from '../UnitPlanetBase';
import { UnitPlanet } from '../UnitPlanet';
import { Global } from '../../Util/Global';
import PoolManager from '../../Ctrl/PoolManager';
import { UICtrl } from '../../Ctrl/UICtrl';
const { ccclass, property } = _decorator;

@ccclass('RocheLimitTipSticker')
export class RocheLimitTipSticker extends GameSticker {
    type: GameStickerType = GameStickerType.RocheLimitTipSticker;
    setTarget(target) {
        this.sun = target as UnitPlanetBase;
    }
    // @property(Node)
    // public textNode:Node = null;
    @property(Label)
    public message: Label = null;

    public forward: Vec2 = new Vec2(0, 1);
    public planet: UnitPlanet = null;
    public sun: UnitPlanetBase = null;

    private _toward: Vec3 = new Vec3;
    update(deltaTime: number) {
        this.planet = Global.gameManager.playerUnit;
        if (!this.planet || this.planet.death) {
            PoolManager.putNode('RocheLimitTipSticker', this.node);
            return;
        }

        // if (!this.planet.parentUnit || this.planet.parentUnit.unitType < UnitType.UnitSun) {
        //     PoolManager.putNode('RocheLimitTipSticker', this.node);
        //     return;
        // }

        // this.sun = this.planet.FilterOtherImpactPlanet_One((planet) => {
        //     return planet.unitType == UnitType.UnitSun;
        // })

        //行星被锁定
        if (!this.sun || this.sun.death || this.planet.forceTrackTarget) {
            PoolManager.putNode('RocheLimitTipSticker', this.node);
            return;
        }
        let toward_v2 = this.planet.unitWorldPos_v2.subtract(this.sun.unitWorldPos_v2);
        let limit_distance = this.sun.CalculateRocheLimitWithTarget(this.planet);
        let current_distance = toward_v2.length();
        let color = null;
        if (current_distance > limit_distance && limit_distance > this.sun.collider.radius + this.planet.collider.radius) {
            color = Color.YELLOW;
            this.message.string = '极限半径';
        }
        else {
            if (this.planet.parentUnit != this.sun) {
                this.message.node.active = false;
                return;
            }
            color = Color.RED;
            this.message.string = '撞击警告';
        }

        this.message.color = color;

        if (!isNaN(limit_distance) && limit_distance > 0) {
            UICtrl.CircleRenderer(this.sun.unitWorldPos, limit_distance / Global.CameraOrthoScale, color);
        }

        this.message.node.active = limit_distance > 0;

        let sun_pos = this.sun.unitWorldPos;
        this._toward.set(toward_v2.x, toward_v2.y);

        let angle = this.forward.signAngle(toward_v2) * 180 / Math.PI;
        this.node.setRotationFromEuler(0, 0, angle); //朝向目标1
        this._toward.normalize().multiplyScalar(limit_distance);
        sun_pos.add(this._toward);
        this.node.setWorldPosition(sun_pos);
    }

    protected onDisable(): void {
        this.planet = null;
        this.sun = null;
    }
}